/**
 * -*- coding:utf-8 -*-
 *
 * @time: 2025/11/4 11:58
 * @author:lhl
 * @编辑器: IntelliJ IDEA
 */
package com.ljs.Dao.util;

import java.sql.*;
import java.util.ResourceBundle;

public class JDBCUtils {
    private static final String DRIVER;
    private static final String URL;
    private static final String USER;
    private static final String PASSWORD;



    static {
        ResourceBundle jdbc = ResourceBundle.getBundle("jdbc");
        DRIVER= jdbc.getString("jdbc.driverName");
        URL=jdbc.getString("jdbc.url");
        USER =jdbc.getString( "jdbc.username");
        PASSWORD=jdbc.getString( "jdbc.password");

        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    public static Connection getConnection(){
        try {
            Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);
            return  connection;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void closeAll(ResultSet resultSet,Statement statement,Connection connection){
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }


}
